package es.ats.codington.web.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class EventByPlaceSServlet
 */
@WebServlet("/EventByPlaceSServlet")
public class EventByPlaceSServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventByPlaceSServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EventService es = new EventService();
		PlaceService ps = new PlaceService();
		Event event = new Event();
		ArrayList<Integer> idsevent = new ArrayList<Integer>();
		ArrayList<Integer> idsplace = new ArrayList<Integer>();
		ArrayList<Event> allevents = new ArrayList<Event>();
		int[] iter = new int[4];
		int[] size = new int[4];
		
		try{
			String type1 = (String) request.getParameter("type1");
			
			idsplace= ps.getIdsPlace(type1);
			size[0]= idsplace.size();
			
			for (iter[0]=0; iter[0]<size[0]; iter[0]++){
				int id= idsplace.get(iter[0]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);
				}
			}
			int sizz = allevents.size();
	
			
			String type2 = (String) request.getParameter("type2");
			
			idsplace= ps.getIdsPlace(type2);
			size[1]= idsplace.size();
			
			for (iter[1]=0; iter[1]<size[1]; iter[1]++){
				int id= idsplace.get(iter[1]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz2 = allevents.size();

			
			String type3 = (String) request.getParameter("type3");
			idsplace= ps.getIdsPlace(type3);
			size[2]= idsplace.size();
			
			for (iter[2]=0; iter[2]<size[2]; iter[2]++){
				int id= idsplace.get(iter[2]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz3 = allevents.size();

			
			String type4 = (String) request.getParameter("type4");
			idsplace= ps.getIdsPlace(type4);
			size[3]= idsplace.size();
			
			for (iter[3]=0; iter[3]<size[3]; iter[3]++){
				int id= idsplace.get(iter[3]);
				idsevent = es.getIdsEvent(id);
				int iter2;
				int size2;
				size2 = idsevent.size();
				for (iter2=0; iter2<size2; iter2++){
					event = es.viewEvent(idsevent.get(iter2));
					allevents.add(event);			
				}
			}
			int sizz4 = allevents.size();
				

			int siz= allevents.size();
			request.setAttribute("size", siz);
			request.setAttribute("events", allevents);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/SouthEvents.jsp"); 
			dispatcher.forward(request, response);  
		
		}catch(Exception ex){
			ex.getMessage();
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
